package com.imddysc.jzknavigator.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.imddysc.jzknavigator.service.ZknavigatorService;
import com.imddysc.jzknavigator.utils.ZknaviUtils;

@Controller
public class ZknavigatorEditController {
	
	private static final Logger logger = LoggerFactory.getLogger(ZknavigatorEditController.class);
	
	@Autowired
	private ZknavigatorService zknavigatorService;
	
	@RequestMapping(value = "/edit/{path}")
	public ModelAndView edit(@PathVariable("path") String path) {
		ModelAndView mav = new ModelAndView();
		logger.info("edit: " + path);
		Map pahtMap = ZknaviUtils.pathHref(path);
		mav.addObject("pathOldUrl",pahtMap.get("pathOldUrl"));
		mav.addObject("pathUrl",pahtMap.get("pathUrl"));
		
		String path1 = path.replace("|","/");
		String path2 = path1.replace("//","/");
		
		try {
			String content = zknavigatorService.getData(path2);
			mav.addObject("content", content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mav.setViewName("edit");
		return mav;
	}

}
